/**
 * Copyright © 2016-2025 The Thingsboard Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.terracloud.server.dao.sql.device;

import com.terracloud.server.common.data.device.DeviceAccessControlMode;
import com.terracloud.server.common.data.device.DeviceAccessControlRule;
import com.terracloud.server.common.data.device.DeviceAccessControlRuleType;
import com.terracloud.server.common.data.id.TenantId;
import com.terracloud.server.common.data.page.PageData;
import com.terracloud.server.common.data.page.PageLink;
import com.terracloud.server.dao.DaoUtil;
import com.terracloud.server.dao.TenantEntityDao;
import com.terracloud.server.dao.device.DeviceAccessControlRuleDao;
import com.terracloud.server.dao.model.sql.DeviceAccessControlRuleEntity;
import com.terracloud.server.dao.sql.JpaAbstractDao;
import com.terracloud.server.dao.util.SqlDao;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@Component
@Slf4j
@SqlDao
public class JpaDeviceAccessControlRuleDao extends JpaAbstractDao<DeviceAccessControlRuleEntity, DeviceAccessControlRule> implements DeviceAccessControlRuleDao, TenantEntityDao<DeviceAccessControlRule> {

    @Autowired
    private DeviceAccessControlRuleRepository deviceAccessControlRuleRepository;

    @Override
    protected Class<DeviceAccessControlRuleEntity> getEntityClass() {
        return DeviceAccessControlRuleEntity.class;
    }

    @Override
    protected JpaRepository<DeviceAccessControlRuleEntity, UUID> getRepository() {
        return deviceAccessControlRuleRepository;
    }

    @Override
    public DeviceAccessControlRule save(TenantId tenantId, DeviceAccessControlRule rule) {
        return super.save(tenantId, rule);
    }

    @Override
    public PageData<DeviceAccessControlRule> findAll(PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findAll(DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTextSearch(String textSearch, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTextSearch(textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTypeAndTextSearch(String type, String textSearch, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTypeAndTextSearch(ruleType, textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByModeAndTextSearch(DeviceAccessControlMode mode, String textSearch, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByModeAndTextSearch(mode, textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTypeAndModeAndTextSearch(String type, DeviceAccessControlMode mode, String textSearch, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTypeAndModeAndTextSearch(ruleType, mode, textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByType(String type, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByType(ruleType, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByMode(DeviceAccessControlMode mode, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByMode(mode, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTypeAndMode(String type, DeviceAccessControlMode mode, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTypeAndMode(ruleType, mode, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public DeviceAccessControlRule findByTenantIdAndValue(UUID tenantId, String value) {
        return DaoUtil.getData(deviceAccessControlRuleRepository.findByTenantIdAndValue(tenantId, value));
    }

    @Override
    public List<DeviceAccessControlRule> findByValueAndType(String value, DeviceAccessControlRuleType type) {
        return DaoUtil.convertDataList(deviceAccessControlRuleRepository.findByValueAndType(value, type));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantId(UUID tenantId, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantId(tenantId, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndTextSearch(UUID tenantId, String textSearch, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndTextSearch(tenantId, textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndType(UUID tenantId, String type, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndType(tenantId, ruleType, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndTypeAndTextSearch(UUID tenantId, String type, String textSearch, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndTypeAndTextSearch(tenantId, ruleType, textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndMode(UUID tenantId, DeviceAccessControlMode mode, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndMode(tenantId, mode, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndModeAndTextSearch(UUID tenantId, DeviceAccessControlMode mode, String textSearch, PageLink pageLink) {
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndModeAndTextSearch(tenantId, mode, textSearch, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndTypeAndMode(UUID tenantId, String type, DeviceAccessControlMode mode, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndTypeAndMode(tenantId, ruleType, mode, DaoUtil.toPageable(pageLink)));
    }

    @Override
    public PageData<DeviceAccessControlRule> findByTenantIdAndTypeAndModeAndTextSearch(UUID tenantId, String type, DeviceAccessControlMode mode, String textSearch, PageLink pageLink) {
        DeviceAccessControlRuleType ruleType;
        try {
            ruleType = DeviceAccessControlRuleType.valueOf(type.toUpperCase());
        } catch (IllegalArgumentException e) {
            throw new IllegalArgumentException("Invalid device access control rule type: " + type);
        }
        return DaoUtil.toPageData(deviceAccessControlRuleRepository.findByTenantIdAndTypeAndModeAndTextSearch(tenantId, ruleType, mode, textSearch, DaoUtil.toPageable(pageLink)));
    }
}